package com.baizhi.service.Impl;

import com.baizhi.dao.OptionDao;
import com.baizhi.dao.TitleDao;
import com.baizhi.entity.Option;
import com.baizhi.entity.Title;
import com.baizhi.service.TitleService;
import com.baizhi.util.Bto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class TitleServiceImpl implements TitleService {
    @Autowired
    private TitleDao td;
    @Autowired
    private OptionDao od;
    @Override
    public PageInfo<Title> selectPage(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Title> titles = td.selectAll();
        return new PageInfo<>(titles);
    }

    @Override
    public void insertAll(Bto bto) {
        Title title = new Title(bto.gettId(), bto.gettTitleName(), bto.gettStopTime());
        td.insertTitle(title);
        bto.setOtitleId(title.getTitleId());
        System.out.println(title.getTitleId());
        od.insertOption(new Option(bto.getoNd(),bto.getoName(),bto.getOtitleId()));
        od.insertOption(new Option(bto.getoNd(),bto.getoName2(),bto.getOtitleId()));
        od.insertOption(new Option(bto.getoNd(),bto.getoName3(),bto.getOtitleId()));
    }
}
